//获取应用实例
var app = getApp();
var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');
var dateExt = require('../../utils/date-extension');
// pages/guesspacket/guesspacket.js
Page({

  /**
   * 页面的初始数据
   */

  data: {
    tradeNo: '',
    topicRecordInfo: null,
    radioInfo: null,
    crosswordInfo: null,
    crosswordInfo: null,
    avatarUrl: '',
    name: '',
    tab1: 'none',
    tab2: 'block',
    select: '-',
    answerRecordList: null,
    array: [],
    index: 0,
    vipId:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getUserInfo(function (res) {
      that.setData({
        userInfo: res
      });
    });
    this.setData({
      tradeNo: options.tradeNo
    });
    
    this.getHbTheTopicRecordInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onPullDownRefresh: function () {
    this.getHbTheTopicRecordInfo();
    wx.stopPullDownRefresh();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '秒趣派',
      desc: '最具人气的答题红包!',
      path: 'pages/guesspacket/guesspacket?tradeNo=' + this.data.tradeNo
    }
  },
  txAmount: function () {
    wx.navigateTo({
      url: '../widthdraw/widthdraw'
    })
  },
  getHbTheTopicRecordInfo: function () {
    var that = this;
    var method = route.getHbTheTopicRecordInfo();
    debugger
    var params = {
      tradeNo: that.data.tradeNo,
      userId: app.globalData.userInfo.id
    };
    httpClient.request(method, 'GET', params, function (res, err) {
      var questions = res.hbQuestions;
      debugger
      var list = res.answerRecordList;
      if (list.length > 0) {
        for (var i = 0; i < list.length; i++) {
          if (list[i].amount > 0 && that.data.vipId==0){ 
            that.setData({
              vipId: list[i].id
            })
          }
          list[i].createTimeStr = new Date(Date.parse(list[i].createTimeStr.replace(/-/g, "/"))).pattern("d日 HH:mm:ss");
        }
      }
      that.setData({
        avatarUrl: res.imgUrl,
        name: res.name,
        topicRecordInfo: res,
        answerRecordList: list
      });
      if (questions.questionsType == 0) {
        that.setData({
          tab1: 'none',
          tab2: 'block',
          radioInfo: res
        });
      } else {
        that.setData({
          tab1: 'block',
          tab2: 'none',
          crosswordInfo: res
        });
      }
      var data = res.hbQuestions.answer;
      var _TheArray = new Array();
      for (var i = 0, len = data.length; i < len; i++) {
        _TheArray[i] = { message: '' };
      }
      that.setData({
        array: _TheArray,
      });

      if (res.statusRecord != 0) {
        var strArray = that.data.array;
        var data = res.hbQuestions.answer;
        for (var i = 0; i < strArray.length; i++) {
          var value = data.charAt(i);
          strArray[i].message = value;
        }
        that.setData({
          array: strArray,
        });
      }

    });
  },
  answerchoose: function (event) {
    //获取当前item的下标id  通过currentTarget.id
    var index = event.currentTarget.dataset.index;
    this.setData({
      select: index,
    });
  },
  tapclick: function (e) {
    var value = e.currentTarget.dataset.val;
    var index = this.data.index;
    var array = this.data.array;
    for (var i = 0; i < array.length; i++) {

      if (index == i) {
        array[i].message = value;
        continue;
      }
    }
    if (index + 1 == array.length) {
      this.setData({
        array: array
      })

      this.addAnswerRecord()

    } else {
      this.setData({
        array: array,
        index: index + 1
      })
    }
  },
  setIndex: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      index: index
    })
  },
  addAnswerRecord: function (e) {
    var that = this;
    var method = route.addAnswerRecord();
    var params = '';
    if (that.data.topicRecordInfo.hbQuestions.questionsType == 1) {
      params = {
        answer: that.data.array,
        topicRecordId: that.data.topicRecordInfo.id,
        userId: app.globalData.userInfo.id
      };
    } else {
      params = {
        answer: that.data.select,
        topicRecordId: that.data.topicRecordInfo.id,
        userId: app.globalData.userInfo.id
      };
    }
    httpClient.request(method, 'POST', params, function (res, err) {
      if (res.statusRecord == 1) {
        if (that.data.answerRecordList) {
          that.data.answerRecordList.unshift(res)
          that.setData({
            answerRecordList: that.data.answerRecordList
          });
        } else {
          var mycars = new Array()
          mycars[0] = res;
          var list = res.answerRecordList; 
          if (res.amount>0){
            that.setData({
              vipId: res.id, 
            });
          }
          that.setData({ 
            answerRecordList: mycars
          });
        }
      }
      var topicRecordInfo = that.data.topicRecordInfo;
      topicRecordInfo.amountRecord = res.amount;
      topicRecordInfo.statusRecord = res.statusRecord;
      that.setData({
        topicRecordInfo: topicRecordInfo
      });


    });
  },
  gotoPerson: function (event) {
    wx.navigateTo({
      url: '../widthdraw/widthdraw'
    })
    /*
    wx.switchTab({
      url: '../widthdraw/widthdraw'
    })
    */
  },
  index: function (event) {
    wx.navigateBack({})
    /*
      wx.switchTab({
        url: '../index/index'
      })
      */
  },
  sharePage: function (event) {
    var tradeNo = this.data.tradeNo;
    wx.navigateTo({
      url: '../creatpacket/creatpacket?tradeNo=' + tradeNo
    })
  },
  getQuestion: function () {
    wx.navigateTo({
      url: '../index/index?questionsType=' + this.data.questionsType
    })
  }
})